package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @RequestMapping
    public Result list() {
        List<Dept>deptlist=deptService.list();
        return Result.success(deptlist);
    }

    @DeleteMapping
    public Result delete(Integer id){
       log.info("delete"+id);
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("add"+dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询, id=" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("update"+dept);
        deptService.update(dept);
        return Result.success();
    }
}
